﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Feicui.BeautyCRM.Common.Log
{
    public class LogHelper
    {
        /// <summary>
        /// 定义一个队列
        /// </summary>
        private static Queue<string> ExceptionStringQueue = new Queue<string>();

        static LogHelper()
        {
            WriterLogAction.GetWriterLogDel();
            
            //通过线程处理日志
            ThreadPool.QueueUserWorkItem(o =>
            {
                while (true)
                {
                    lock (ExceptionStringQueue)
                    {
                        //有日志就写入
                        if (ExceptionStringQueue.Count > 0)
                        {
                            WriterLogAction.WriterLogDel(ExceptionStringQueue.Dequeue());
                        }
                        else
                        {
                            //休眠0.5秒
                            Thread.Sleep(500);
                        }
                    }
                }
            });
        }

        /// <summary>
        /// 将日志写入队列中
        /// </summary>
        /// <param name="exceptionText"></param>
        public static void WriteLog(string exceptionText)
        {
            lock (ExceptionStringQueue)
            {
                ExceptionStringQueue.Enqueue(exceptionText);
            }
        }
    }
}
